package com.manu.mdatepickersample;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.manu.mdatepicker.AgePickerDialog;
import com.manu.mdatepicker.DatePickerDialog;
import com.manu.mdatepicker.LevelPickerDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 日期选择器（年月日时分）
        Button btn_date = findViewById(R.id.btn_date);
        btn_date.setOnClickListener(v -> {
            DatePickerDialog dialog = new DatePickerDialog.Builder(this)
                    .setCanceledTouchOutside(true)
                    .setSupportTime(true)
                    .setTwelveHour(true)
                    .setCanceledTouchOutside(true) // true点击外部消失
                    .setOnDateResultListener(date -> {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(date);
                        SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
                        dateFormat.applyPattern("yyyy年 MM 月 dd日 HH时 mm分");
                        btn_date.setText(dateFormat.format(new Date(date)));
                    })
                    .build();
            dialog.show();
        });

        // 年龄选择器
        Button btn_age = findViewById(R.id.btn_age);
        btn_age.setOnClickListener(v -> {
            @SuppressLint("SetTextI18n")
            AgePickerDialog dialog = new AgePickerDialog.Builder(this)
                    .setCanceledTouchOutside(true)
                    .setGravity(Gravity.CENTER)
                    .setCanceledTouchOutside(true)  // true点击外部消失
                    .setOnDateResultListener((min_age, max_age) -> {
                        if (min_age < max_age) {
                            btn_age.setText(min_age + "岁 至 " + max_age + "岁");
                        } else {
                            btn_age.setText(max_age + "岁 至 " + min_age + "岁");
                        }

                    })
                    .build();
            dialog.show();
        });

        // 等级选择器
        Button btn_level = findViewById(R.id.btn_level);
        btn_level.setOnClickListener(v -> {
            @SuppressLint("SetTextI18n")
            LevelPickerDialog dialog = new LevelPickerDialog.Builder(this)
                    .setCanceledTouchOutside(true)
                    .setGravity(Gravity.CENTER)
                    .setCanceledTouchOutside(true)  // true点击外部消失
                    .setOnDateResultListener((min_level, max_level) -> {
                        if (min_level < max_level) {
                            btn_level.setText(min_level + "级 至 " + max_level + "级");
                        } else {
                            btn_level.setText(max_level + "级 至 " + min_level + "级");
                        }
                    })
                    .build();
            dialog.show();
        });

        // 日期选择器（年月日）
        Button btn_birthday = findViewById(R.id.btn_birthday);
        btn_birthday.setOnClickListener(v -> {
            DatePickerDialog dialog = new DatePickerDialog.Builder(this)
                    .setCanceledTouchOutside(true)
                    .setTitle(getResources().getString(R.string.strBirthdaySelect))
                    .setSupportTime(false)          // false： 不支持时间
                    .setCanceledTouchOutside(true)  // true点击外部消失
                    .setOnDateResultListener(date -> {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(date);
                        SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
                        dateFormat.applyPattern("yyyy年 MM 月 dd日");
                        btn_birthday.setText(dateFormat.format(new Date(date)));
                    })
                    .build();
            dialog.show();
        });


    }




}
